<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/4/17
 * Time: 17:45
 */

namespace app\admin\controller;


use app\admin\model\OrderGoods;
use app\admin\model\UsersApply;
use app\AdminRest;
use think\App;
use think\facade\View;
use app\admin\model\Users as UsersModel;

class Users extends AdminRest
{
    protected $model;

    public function __construct(App $app, UsersModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    public function addUser()
    {
        try {
            validate($this->model->rule, $this->model->message)->check($this->_param);
            $data = $this->_param;
            $data['create_time'] = $data['update_time'] = time();
            $this->model->save($data);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function usersList()
    {
        $needData = $this->_param['needData'] ?? 0;
        switch ($needData) {
            case  1:
                $keyword = $phone = '';
                if (isset($this->_param['keyword']) && !empty($this->_param['keyword']))
                    $keyword = $this->_param['keyword'];
                if (isset($this->_param['phone']) && !empty($this->_param['phone']))
                    $phone = $this->_param['phone'];

                $where = ['is_deleted' => 0];
                if (isset($this->_param['status']) && !empty($this->_param['status']))
                    $where['status'] = $this->_param['status'];
                $users = $this->model->when(!empty($keyword),
                    function ($q) use ($keyword) {
                        $q->where('userName', 'like', "%$keyword%");
                    })->when(!empty($phone),
                    function ($q) use ($phone) {
                        $q->where('userTel', 'like', "%$phone%");
                    })
                    ->where($where)->order('id', 'desc')->paginate($this->_param['size'] ?? 10)->toArray();
                return $this->success($users);
                break;
            default:
                return View::fetch('usersList');
                break;
        }
    }


    public function editUser()
    {
        $data = $this->_param;
        unset($data['id']);
        if (empty($this->_param['id']) || $this->_param['id'] < 0)
            return $this->error('数据有误');
        try {
            validate($this->model->rule, $this->model->message)->check($data);
            $data['update_time'] = time();
            if (isset($data['deleted']) && $data['deleted'] == 1) {
                $data['delete_time'] = $data['update_time'];
            }
            unset($data['create_time']);
            $this->model->where(['id' => $this->_param['id']])->save($data);
            return $this->success('修改成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function applyList(UsersApply $model)
    {
        $needData = $this->_param['needData'] ?? 0;
        switch ($needData) {
            case  1:
                $keyword = '';
                if (isset($this->_param['keyword']) && !empty($this->_param['keyword']))
                    $keyword = $this->_param['keyword'];

                $where = ['deleted' => 0];
                if (isset($this->_param['status']) && !empty($this->_param['status']))
                    $where['status'] = $this->_param['status'];
                $applies = $model->when(!empty($keyword),
                    function ($q) use ($keyword) {
                        $q->where('apply_content', 'like', "%$keyword%");
                    })
                    ->where($where)->order('id', 'desc')->paginate($this->_param['size'] ?? 10)->toArray();
                $userIds = array_column($applies['data'], 'user_id');
                $users = $this->model->whereIn('id', $userIds)->select()->toArray();
                $users = array_column($users, null, 'id');
                foreach ($applies['data'] as &$v) {
                    $user = $users[$v['user_id']] ?? [];
                    $v['userTel'] = $user['userTel'] ?? '';
                    $v['email'] = $user['email'] ?? '';
                }
                return $this->success($applies);
                break;
            default:
                return View::fetch('applyList');
                break;
        }
    }

    public function editApply(UsersApply $model)
    {
        $data = [
            'user_id' => $this->_param['user_id'],
            'apply_content' => $this->_param['apply_content'],
            'status' => $this->_param['status']
        ];
        unset($data['id']);
        if (empty($this->_param['id']) || $this->_param['id'] < 0)
            return $this->error('数据有误');
        try {
            validate($model->rule, $model->message)->check($data);
            $data['update_time'] = time();
            if (isset($data['deleted']) && $data['deleted'] == 1) {
                $data['delete_time'] = $data['update_time'];
            }
            unset($data['create_time']);
            $model->where(['id' => $this->_param['id']])->save($data);
            if ($data['status'] == 1) {
                $this->model->where(['id' => $data['user_id']])->save(['is_wholesale' => 1]);
            }
            return $this->success('修改成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

}
